package com.muchfish.webflux.http;

import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;

public class SimpleHttpHandlerServerExample {

    public static void main(String[] args) throws IOException {
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(
                RouterFunctions.route()
                        .GET("/hello", request -> ServerResponse.ok().bodyValue("Hello, WebFlux!"))
                        .build());

        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);

        //启动Netty服务器
        HttpServer.create()
                .host("localhost")
                .port(8080)
                .handle(adapter) //用指定的处理器处理请求
                .bindNow(); //现在就绑定

        System.out.println("服务器启动完成....监听8080，接受请求");
        System.in.read();
        System.out.println("服务器停止....");
    }
}

